﻿// CustomExcel.cpp : 实现文件
//

#include "stdafx.h"
#include "BtsClient.h"
#include "CustomExcel.h"
#include "afxdialogex.h"


// CCustomExcel 对话框

IMPLEMENT_DYNAMIC(CCustomExcel, CDialogEx)

CCustomExcel::CCustomExcel(CWnd* pParent /*=NULL*/)
	: CDialogEx(CCustomExcel::IDD, pParent)
	,m_nData(0)
	,m_nStep(0)
	,m_nCycle(0)
	,m_nSel(0)
{

}

CCustomExcel::~CCustomExcel()
{
}

void CCustomExcel::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SHEET_LIST, m_Table);
	DDX_Control(pDX, IDC_ITEM_LIST, m_Item);
}


BEGIN_MESSAGE_MAP(CCustomExcel, CDialogEx)
	ON_BN_CLICKED(IDOK, &CCustomExcel::OnBnClickedOk)
	ON_NOTIFY(NM_CLICK, IDC_SHEET_LIST, &CCustomExcel::OnNMClickSheetList)
	ON_NOTIFY(NM_KILLFOCUS, IDC_SHEET_LIST, &CCustomExcel::OnNMKillfocusSheetList)
	ON_NOTIFY(NM_SETFOCUS, IDC_SHEET_LIST, &CCustomExcel::OnNMSetfocusSheetList)
	ON_NOTIFY(NM_CLICK, IDC_ITEM_LIST, &CCustomExcel::OnNMClickItemList)
	ON_NOTIFY(NM_DBLCLK, IDC_SHEET_LIST, &CCustomExcel::OnNMDblclkSheetList)
	ON_NOTIFY(NM_DBLCLK, IDC_ITEM_LIST, &CCustomExcel::OnDblclkItemList)
END_MESSAGE_MAP()


// CCustomExcel 消息处理程序


void CCustomExcel::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}




BOOL CCustomExcel::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_Table.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES);
	m_Item.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES|LVS_SINGLESEL|LVS_EX_SIMPLESELECT);
	m_Table.InsertItem(0,_T("详细信息"));
	m_Table.InsertItem(1,_T("工步"));
	m_Table.InsertItem(2,_T("循环"));
	for(int i=0;i<3;i++)
		if(m_Select>>i & 1)
			m_Table.SetCheck(i);
	// TODO:  在此添加额外的初始化
	ShowList(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CCustomExcel::ShowList(int Num)
{
	int flag = 0;
	m_nSel = Num;
	DWORD dwPos = GetMessagePos();
	CPoint pt(dwPos);
	m_Table.ScreenToClient(&pt);
	UINT nFlag;
	int nItem = m_Table.HitTest(pt,&nFlag);
	if(nFlag == LVHT_ONITEMSTATEICON)
	{
		BOOL bCheckState = m_Table.GetCheck(nItem);
		if(bCheckState)
			flag = 1;
		else
			flag = 2;
	}
	if(Num == 0)
	{
		m_Item.DeleteAllItems();
		m_Item.InsertItem(0,_T("序号"));
		m_Item.InsertItem(1,_T("工步时间"));
		m_Item.InsertItem(2,_T("记录时间"));
		m_Item.InsertItem(3,_T("循环"));
		m_Item.InsertItem(4,_T("工步序号"));
		m_Item.InsertItem(5,_T("工步号"));
		m_Item.InsertItem(6,_T("工步类型"));
		m_Item.InsertItem(7,_T("电压"));
		m_Item.InsertItem(8,_T("电流"));
		m_Item.InsertItem(9,_T("温度"));
		m_Item.InsertItem(10,_T("容量"));
		m_Item.InsertItem(11,_T("充电容量"));
		m_Item.InsertItem(12,_T("放电容量"));
		//m_Item.InsertItem(13,_T("电阻"));
		//m_Item.InsertItem(14,_T("时间"));
		if(flag == 0)
			for(int i =0;i < 15;i++)
				m_Item.SetCheck(i,m_nData&1<<i);
		else if(flag == 1)
		{
			m_nData = 0;
			m_Select &= ~(1<<Num);
		}
		else if(flag == 2)
		{
			for(int i =0;i < 15;i++) 
				m_Item.SetCheck(i);
			m_nData = 0x00001FFF;
			m_Select |= 1<<Num;
		}
	}
	else if(Num == 1)
	{
		m_Item.DeleteAllItems();
		m_Item.InsertItem(0,_T("序号"));
		m_Item.InsertItem(1,_T("工步类型"));
		m_Item.InsertItem(2,_T("工步时间"));
		m_Item.InsertItem(3,_T("温度"));
		m_Item.InsertItem(4,_T("终止电压"));
		m_Item.InsertItem(5,_T("终止容量"));
		m_Item.InsertItem(6,_T("容量"));
		//m_Item.InsertItem(7,_T("充放电时间"));
		if(flag == 0)
			for(int i =0;i < 8;i++)
				m_Item.SetCheck(i,m_nStep&1<<i);
		else if(flag == 1)
		{
			m_nStep = 0;
			m_Select &= ~(1<<Num);
		}
		else if(flag == 2)
		{
			for(int i =0;i < 8;i++) 
				m_Item.SetCheck(i);
			m_nStep = 0x0000007F;
			m_Select |= 1<<Num;
		}

	}
	else if(Num == 2)
	{
		m_Item.DeleteAllItems();
		m_Item.InsertItem(0,_T("序号"));
		m_Item.InsertItem(1,_T("循环"));
		m_Item.InsertItem(2,_T("充电容量"));
		m_Item.InsertItem(3,_T("充电时间"));
		m_Item.InsertItem(4,_T("放电容量"));
		m_Item.InsertItem(5,_T("放电时间"));
		if(flag == 0)
			for(int i =0;i < 6;i++)
				m_Item.SetCheck(i,m_nCycle&1<<i);
		else if(flag == 1)
		{
			m_nCycle = 0;
			m_Select &= ~(1<<Num);
		}
		else if(flag == 2)
		{
			for(int i =0;i < 6;i++) 
				m_Item.SetCheck(i);
			m_nCycle = 0x0000003F;
			m_Select |= 1<<Num;
		}
	}
}

void CCustomExcel::OnNMClickSheetList(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	ShowList(pNMItemActivate->iItem);
	*pResult = 0;
}


void CCustomExcel::OnNMKillfocusSheetList(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	m_nSelItem = m_Table.GetSelectionMark();
	m_Table.SetItemState(m_nSelItem, LVIS_DROPHILITED, LVIS_DROPHILITED);
	*pResult = 0;
}


void CCustomExcel::OnNMSetfocusSheetList(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	m_Table.SetItemState(m_nSelItem, FALSE, LVIF_STATE);
	*pResult = 0;
}


void CCustomExcel::OnNMClickItemList(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	int Num = pNMItemActivate->iItem;
	DWORD dwPos = GetMessagePos();
	CPoint pt(dwPos);
	m_Item.ScreenToClient(&pt);
	UINT nFlag;
	int nItem = m_Item.HitTest(pt,&nFlag);
	if(nFlag == LVHT_ONITEMSTATEICON)
	{
		BOOL bCheckState = m_Item.GetCheck(nItem);
		if(bCheckState)
		{
			if(m_nSel == 0)
				m_nData &= ~(1<< Num);
			else if(m_nSel == 1)
				m_nStep &= ~(1<<Num);
			else if(m_nSel == 2)
				m_nCycle &= ~(1<<Num);
		}
		else
		{
			if(m_nSel == 0)
			{
				m_nData |= 1<<Num;
			}
			else if(m_nSel == 1)
			{
				m_nStep |= 1<<Num;
			}
			else if(m_nSel == 2)
			{
				m_nCycle |= 1<<Num;
			}
			m_Table.SetCheck(m_nSel);
			m_Select |= 1<<m_nSel;
		}
	}
	*pResult = 0;
}


void CCustomExcel::OnNMDblclkSheetList(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	OnNMClickSheetList(pNMHDR,pResult);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CCustomExcel::OnDblclkItemList(NMHDR *pNMHDR, LRESULT *pResult)//点击速度过快
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	OnNMClickItemList(pNMHDR,pResult);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
